package com.scau.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.scau.dao.AdminDao;
import com.scau.dao.LoginDataDao;
import com.scau.dao.StudentDao;
import com.scau.dao.TeacherDao;
import com.scau.pojo.Admin;
import com.scau.pojo.Logindata;
import com.scau.pojo.Student;
import com.scau.pojo.Teacher;
import com.scau.service.StudentService;
import com.scau.service.TeacherService;

@RestController
@RequestMapping("/userController")
public class UserController {
	
	@Autowired
	StudentService studentService;
	
	@Autowired
	StudentDao studentdao;
	
	@Autowired
	TeacherService teacherService;
	
	@Autowired
	TeacherDao teacherdao;
	
	@Autowired
	AdminDao admindao;
	
	@Autowired
	LoginDataDao logindatadao;
	
    @RequestMapping("/login")
    public Map<String,Object> login(@RequestBody JSONObject jsonObject,HttpServletRequest request) throws Exception{
    	String role=jsonObject.getString("role");
    	String name=jsonObject.getString("username");
    	String password=jsonObject.getString("password");
    	String nickname="";
    	int id=-1;
    	HttpSession session = request.getSession();
    	Map<String,Object> resultMap=new HashMap<String,Object>();
    	Map<String,Object> map=new HashMap<String,Object>();
    	try {
    		switch (role) {
    		case "student":
    			session.setAttribute("role", "student");
    			map=studentdao.getStudentByName(name);
    			Student student=(Student)map.get("student");
    			if(student!=null){
    				id=student.getStId();
    				nickname=student.getStNickname();
    				if(password.equals(student.getStPassword())){
    					resultMap.put("user", student);
    					resultMap.put("resultCode", 1);
    				}else{
    					resultMap.put("resultCode", 0);
    				}
    			}else{
    				resultMap.put("user", null);
    				resultMap.put("resultCode", -1);
    			}
    			break;
    		case "teacher":
    			session.setAttribute("role", "teacher");
    			map=teacherdao.getTeacherByName(name);
    			Teacher teacher=(Teacher)map.get("teacher");
    			if(teacher!=null){
    				id=teacher.gettId();
    				nickname=teacher.gettNickname();
    				if(password.equals(teacher.gettPassword())){
    					resultMap.put("user", teacher);
    					resultMap.put("resultCode", 1);
    				}else{
    					resultMap.put("resultCode", 0);
    				}
    			}else{
    				resultMap.put("user", null);
    				resultMap.put("resultCode", -1);
    			}
    			break;
    		case "admin" :
    			session.setAttribute("role", "admin");
    			map=admindao.getAdmin(name);
    			Admin admin=(Admin)map.get("admin");
    			if(admin!=null){
    				id=admin.getId();
    				nickname="";
    				if(password.equals(admin.getPassword())){
    					resultMap.put("user", admin);
    					resultMap.put("resultCode", 1);
    				}else{
    					resultMap.put("resultCode", 0);
    				}
    			}else{
    				resultMap.put("user", null);
    				resultMap.put("resultCode", -1);
    			}
    			break;
    		} 
        	if(resultMap.get("user")!=null){
        		int rs=(int)logindatadao.getUserByName(name).get("returnCode");
        		int addLoginRS=-1;
        		Logindata logindata=new Logindata();
        		if(rs==-1){//第一次登陆的用户
        			logindata.setLoginnum(1);
        			logindata.setLogintime(new Date());
        			logindata.setUserId(id);
        			logindata.setUserName(name);
					logindata.setUserNickname(nickname);
        			logindata.setUserType(role);
        			addLoginRS=logindatadao.insert(logindata);
        		}else{
        			logindata=(Logindata)logindatadao.getUserByName(name).get("logindata");
        			logindata.setLoginnum(logindata.getLoginnum()+1);
        			logindata.setLogintime(new Date());
        			addLoginRS=logindatadao.update(logindata);
        			
        		}
        		if(addLoginRS!=1){
    				resultMap.put("addLoginRS", -1);
    				
    			}else{
    				resultMap.put("addLoginRS", 1);
    			}
        		
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
    	
    
    }
    
    @RequestMapping("/isExist")
	public int isExist(@RequestParam String name,@RequestParam String type) throws Exception{
		int result=-1;
		try{
			if(type.contains("student")){
				result=studentdao.isExist(name);
			}else if(type.contains("teacher")){
				result=teacherdao.isExist(name);
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
    
    @RequestMapping("/qryAllLoginData")
	public List<Logindata> qryAllLoginData() throws Exception{
		return logindatadao.query();
	}
}
